﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace xiaolanshu
{
    public partial class LipPanel : UserControl
    {
        public LipPanel()
        {
            InitializeComponent();
        }

        private void LipPanel_Load(object sender, EventArgs e)
        {
            AutoScroll = true;
            AutoScrollMargin = new Size(30, 30);
            BackColor = Color.Transparent;

            Data.getLipData();

            for (int i = 0; i < Data.lipstick_data.Rows.Count; i++)
            {
                LipBox lip_box = new LipBox();
                lip_box.box_data = Data.lipstick_data.Rows[i];
                lip_box.box_data["index"] = i;
                lip_box.reload();

                lip_box.Location = new Point(30 + i % 4 * 200, 30 + i / 4 * 250);
                Console.WriteLine("Location " + i.ToString() + ": " + lip_box.Location.ToString());
                lip_box.Anchor = AnchorStyles.Top;
                Controls.Add(lip_box);
            }
        }

        public enum OrderMode { Price, ClickNum };

        public void order(OrderMode mode)
        {
            DataView dataview = Data.lipstick_data.DefaultView;
            
            switch (mode)
            {
                case OrderMode.Price:
                    dataview.Sort = "price";
                    break;
                case OrderMode.ClickNum:
                    dataview.Sort = "clicknum DESC";
                    break;
            }

            DataTable ordered_data = dataview.ToTable();

            reloadBoxes(ordered_data);
        }

        private void reloadBoxes(DataTable ordered_datad)
        {
            int i = 0;
            foreach (LipBox lip_box in Controls)
            {
                int index = (int)ordered_datad.Rows[i]["index"];
                lip_box.box_data = Data.lipstick_data.Rows[index];
                lip_box.reload();
                i++;
            }
        }
    }
}
